package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.Attribute;
import cn.com.ege.mvc.model.business.Category;
import cn.com.ege.mvc.model.business.Material;
import cn.com.ege.mvc.model.business.MaterialType;
import cn.com.ege.mvc.model.business.Rule;
import cn.com.ege.mvc.service.impl.business.AttributeService;
import cn.com.ege.mvc.service.impl.business.CategoryService;
import cn.com.ege.mvc.service.impl.business.MaterialService;
import cn.com.ege.mvc.service.impl.business.MaterialTypeService;
import cn.com.ege.mvc.service.impl.business.RuleService;
import cn.com.ege.mvc.util.PageController;

public class MaterialAction extends BaseAction implements IAction, Serializable {
	private final static Log logger = LogFactory.getLog(MaterialAction.class);
	private PageController pager = null;
	private MaterialService service = null;
	private Material material = null;
	private List<Material> mList = null;
	private MaterialTypeService typeService = null;
	private List<MaterialType> typeList = null;

	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		if (null != act) {
			// 取列表
			if ("list".equals(act)) {
				int totalRows = service.getRecordCount(new Material());
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				List<Material> resourceList = service.list(new Material(),
						this.pager.getPageStartRow(), pager.getPageSize(),
						"order by id desc", true);
				typeList = typeService.listAll(new MaterialType());
				request.setAttribute("typeList", typeList);
				pager.setData(resourceList);
				return getReturnURL(LIST);
			}
			// 点了添加或点了修改
			else if ("get".equals(act)) {
				material = service.get(material);
				typeList = typeService.listAll(new MaterialType());
				List<Rule> ruleList = (new RuleService()).list(new Rule(), null, null, "and status=1", true);
				List<List<Category>> CList = new ArrayList<List<Category>>();
				List<List<?>> AList = new ArrayList<List<?>>();
				for(int i = 0; i < ruleList.size(); i++){
					List<Category> categoryList = (new CategoryService()).list(new Category(), null, null, "and rule_id="+ruleList.get(i).getId(), true);
					List<List<Attribute>> listAtt = new ArrayList<List<Attribute>>();
					for(int j = 0; j < categoryList.size(); j++) {
						List<Attribute> attributeList = (new AttributeService()).list(new Attribute(), null, null, "and category_id="+categoryList.get(j).getId(), true);
						listAtt.add(attributeList);
					}
					CList.add(categoryList);
					AList.add(listAtt);
				}
				request.setAttribute("cList", CList);
				request.setAttribute("aList", AList);
				request.setAttribute("typeList", typeList);
				request.setAttribute("ruleList", ruleList);
				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {
					service.save(material);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					// 保存原来表单已输入的内容
					request.setAttribute("material", material);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
		}
		return null;
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public MaterialService getService() {
		return service;
	}

	public void setService(MaterialService service) {
		this.service = service;
	}

	public Material getMaterial() {
		return material;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}

	public List<Material> getMList() {
		return mList;
	}

	public void setMList(List<Material> list) {
		mList = list;
	}

	public List<MaterialType> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<MaterialType> typeList) {
		this.typeList = typeList;
	}

	public MaterialTypeService getTypeService() {
		return typeService;
	}

	public void setTypeService(MaterialTypeService typeService) {
		this.typeService = typeService;
	}

}
